#!/bin/bash
#
# Copyright 2014-2015 the Pacemaker project contributors
#
# The version control history for this file may have further details.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of version 2 of the GNU General Public License as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it would be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
#
# Further, this software is distributed without any warranty that it is
# free of the rightful claim of any third person regarding infringement
# or the like.  Any license provided herein, whether implied or
# otherwise, applies only to this software file.  Patent licenses, if
# any, provided herein do not apply to combinations of this program with
# other software, or any other product whatsoever.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write the Free Software Foundation,
# Inc., 59 Temple Place - Suite 330, Boston MA 02111-1307, USA.
#
#######################################################################

port=""
action="list"		 # Default fence action

function usage()
{
cat <<EOF
`basename $0` - A fencing agent for docker containers for testing purposes
 
Usage: `basename $0` -o|--action [-n|--port] [options]
Options:
 -h, --help 		This text
 -V, --version		Version information
 
Commands:
 -o, --action		Action to perform: on|off|reboot|status|monitor
 -n, --port 		The name of a container to control/check

EOF
	exit 0;
}

function metadata()
{
cat <<EOF
<?xml version="1.0" ?>
<resource-agent name="fence_docker_cts" shortdesc="docker fencing agent for testing purposes" >
	<longdesc>
		fence_docker_cts fences docker containers for testing purposes.
	</longdesc>
	<parameters>
	<parameter name="action" unique="1" required="0">
		<getopt mixed="-o, --action=[action]" />
		<content type="string" default="reboot" />
		<shortdesc lang="en">Fencing Action</shortdesc>
	</parameter>
	<parameter name="port" unique="1" required="0">
		<getopt mixed="-n, --port=[port]" />
		<content type="string" />
		<shortdesc lang="en">The name/id of docker container to control/check</shortdesc>
	</parameter>
	</parameters>
	<actions>
	<action name="on" />
	<action name="off" />
	<action name="reboot" />
	<action name="status" />
	<action name="list" />
	<action name="monitor" />
	<action name="metadata" />
	</actions>
</resource-agent>
EOF
	exit 0;
}

function docker_log() {
	if ! [ "$action" = "list" ]; then
		printf "$*\n" 1>&2
	fi
}

# stdin option processing
if [ -z $1 ]; then
	# If there are no command line args, look for options from stdin
	while read line; do
		for word in $(echo "$line"); do
			case $word in 
			option=*|action=*) action=`echo $word | sed s/.*=//`;;
			port=*)			port=`echo $word | sed s/.*=//`;;
			node=*)			port=`echo $word | sed s/.*=//`;;
			nodename=*)			port=`echo $word | sed s/.*=//`;;
			--);;
			*) docker_log "Invalid command: $word";;
			esac
		done
	done
fi

# Command line option processing
while true ; do
	if [ -z "$1" ]; then
		break;
	fi
	case "$1" in
	-o|--action|--option) action=$2;	shift; shift;;
	-n|--port)			port=$2;	  shift; shift;;
	-V|--version) echo "1.0.0"; exit 0;;
	--help|-h) 
		usage;
		exit 0;;
	--) shift ; break ;;
	*) docker_log "Unknown option: $1. See --help for details."; exit 1;;
	esac
done

action=`echo $action | tr 'A-Z' 'a-z'`
case $action in
	hostlist|list) action=list;;
	stat|status)   action=status;;
	restart|reboot|reset)  action=reboot;;
	poweron|on)	action=start;;
	poweroff|off)  action=stop;;
esac

function fence_done() 
{
	if [ $1 -eq 0 ]; then
		docker_log "Operation $action (port=$port) passed"
	else
		docker_log "Operation $action (port=$port) failed: $1"
	fi
	if [ -z "$returnfile" ]; then
		rm -f $returnfile
	fi
	if [ -z "$helperscript" ]; then
		rm -f $helperscript
	fi
	exit $1
}

case $action in 
	metadata) metadata;;
esac

returnfile=$(mktemp /tmp/fence_docker_cts_returnfileXXXX)
returnstring=""
helper_script=$(mktemp /tmp/fence_docker_cts_helperXXXX)

exec_action()
{
	echo "#!/bin/bash" > $helper_script
	echo "sleep 10000" >> $helper_script
	chmod 755 $helper_script
	src="$(uname -n)"

	$helper_script "$src" "$action" "$returnfile" "$port" > /dev/null 2>&1 &
	pid=$!
	docker_log "waiting on pid $pid"
	wait $pid > /dev/null 2>&1
	returnstring=$(cat $returnfile)

	if [ -z "$returnstring" ]; then
		docker_log "fencing daemon did not respond"
		fence_done 1
	fi

	if [ "$returnstring" == "fail" ]; then
		docker_log "fencing daemon failed to execute action [$action on port $port]"
		fence_done 1
	fi

	return 0
}

exec_action
case $action in 
	list)
		cat $returnfile
		fence_done 0
		;;

	status)
		# 0 if container is on
		# 1 if container can not be contacted or unknown
		# 2 if container is off
		if [ "$returnstring" = "true" ]; then
			fence_done 0
		else 
			fence_done 2
		fi
		;;
	monitor|stop|start|reboot) : ;;
	*) docker_log "Unknown action: $action"; fence_done 1;;
esac

fence_done $?
